package servlet.stu;

import bean.Exchange;
import bean.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import dao.UserDaoImpl;
import utils.JwtUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


//学生查看自己的换房申请
@WebServlet("/stu/queryExchangeInfoByStu")
public class QueryExchangeInfoByStuServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user_token = request.getHeader("user_token_header");
        Integer stuId= JwtUtils.getStuIdFromToken(user_token,"stuId");
        Integer currentPage= Integer.valueOf(request.getParameter("currentPage"));
        List<Exchange> exchangeList=UserDaoImpl.queryExchangeInfoByStu(stuId,currentPage);
        Result result = new Result();
        if(exchangeList!=null){
            result.success(exchangeList);
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }else{
            result.fail();
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }
    }
}
